namespace RSCore.Files;
public sealed record class LdstrFile(string Name) : RSFile(Name)
{
	public readonly List<string> Text = new();
	public override void Deserialize(JsonValue json)
	{
		Text.Clear();
		Text.AddRange(json.Qa().Select(s => s.Qs()));
	}

	public override void Load(BinaryReader reader)
	{
		var count = reader.ReadInt32();
		for (int i = 0; i < count; i++)
		{
			Text.Add(reader.ReadString());
		}
	}

	public override void Read(StreamReader reader)
	{
		var text = reader.ReadProperties()
			.OrderBy(pair => int.Parse(pair.Key))
			.Select(pair => pair.Value)
			.ToList();
	}

	public override void Save(BinaryWriter writer)
	{
		int count = Text.Count;
		writer.Write(count);
		for (int i = 0; i < count; i++)
		{
			writer.Write(Text[i]);
		}
	}

	public override JsonValue Serialize()
	{
		return new JsonArray(Text.Select(s => (JsonValue)s));
	}

	public override void Write(StreamWriter writer, RSFile? annotation = null)
	{
		var orig = annotation as LdstrFile;
		string? anno = null;
		for (int i = 0; i < Text.Count; i++)
		{
			if(orig != null && orig.Text.Count > i)
			{
				anno = orig.Text[i];
			}
			writer.WriteProperty(Text[i], anno, $"{i}");
		}
	}
}
